package flex.cc.uac.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import flex.cc.base.enums.ErrorCodeEnum;
import flex.cc.uac.dao.UacRoleMenuDao;
import flex.cc.uac.entity.UacRoleMenuEntity;
import flex.cc.uac.entity.UacRoleUserEntity;
import flex.cc.uac.exceptions.UacBizException;
import flex.cc.uac.service.UacRoleMenuService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;


@Service("uacRoleMenuService")
public class UacRoleMenuServiceImpl extends ServiceImpl<UacRoleMenuDao, UacRoleMenuEntity> implements UacRoleMenuService {

    @Override
    public void deleteByRoleId(Long roleId) {
        if (roleId == null) {
            throw new UacBizException(ErrorCodeEnum.UAC10012001);
        }

        Map<String,Object> map = new HashMap<>();
        map.put("role_id",roleId);
        this.removeByMap(map);
    }

    @Override
    public void deleteByRoleIdList(final List<Long> roleIdList) {
        Wrapper queryWrapper = new QueryWrapper<UacRoleUserEntity>()
                .in("role_id",roleIdList);
        this.remove(queryWrapper);
    }

    @Override
    public List<UacRoleMenuEntity> listByRoleId(Long roleId) {
        Wrapper queryWrapper = new QueryWrapper<UacRoleUserEntity>()
                .eq("role_id",roleId);

        return this.list(queryWrapper);
    }

    @Override
    public void insert(Long roleId, Set<Long> menuIdList) {
        if (roleId == null) {
            throw new UacBizException(ErrorCodeEnum.UAC10012001);
        }
        UacRoleMenuEntity uacRoleMenu = new UacRoleMenuEntity();
        uacRoleMenu.setRoleId(roleId);
        for (Long menuId : menuIdList) {
            uacRoleMenu.setMenuId(menuId);
            this.save(uacRoleMenu);
        }
    }
}